//
//  CustomAlertView.m
//  自定义AlertView
//
//  Created by 丶离人梦 on 2017/2/8.
//  Copyright © 2017年 丶离人梦. All rights reserved.
//

#import "CustomAlertView.h"

@interface CustomAlertView ()

@property(nonatomic,strong)UIView *backgroundView;
@property(nonatomic,strong)UIView *ShowAlertView;
@property(nonatomic,strong)UIButton *CancelButton;
@property(nonatomic,strong)UIButton *OkButton;

@property(nonatomic,strong)NSString *TitleString;
@property(nonatomic,strong)NSString *CancelString;
@property(nonatomic,strong)NSString *OkString;

@end

@implementation CustomAlertView

- (instancetype)initWithTitle:title delegate:delegate cancelButtonTitle:cancelTitle otherButtonTitle:otherTitle {
    if (self = [super initWithFrame:[[UIApplication sharedApplication] keyWindow].frame]) {
        self.TitleString = title;
        self.delegate = delegate;
        self.CancelString = cancelTitle;
        self.OkString = otherTitle;
        
        [self CreateSubView];
    }
    return self;
}

- (void)CreateSubView {
    self.backgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    self.backgroundView.backgroundColor = [UIColor blackColor];
    self.backgroundView.alpha = 0.6;
    [self addSubview:self.backgroundView];
    
    self.ShowAlertView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SYRealValue(630 / 2), SYRealValue(270 / 2))];
    self.ShowAlertView.layer.cornerRadius = 5;
    self.ShowAlertView.backgroundColor = [UIColor whiteColor];
    self.ShowAlertView.center = CGPointMake(ScreenWidth / 2, ScreenHeight / 2);
    [self addSubview:self.ShowAlertView];
    
    //标题
    UILabel *TitleLabel = [[UILabel alloc]init];
    TitleLabel.text = self.TitleString;
    TitleLabel.textAlignment = NSTextAlignmentCenter;
    TitleLabel.font = [UIFont systemFontOfSize:SYRealValue(30 / 2)];
    [self.ShowAlertView addSubview:TitleLabel];
    [TitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.ShowAlertView).with.offset(SYRealValue(70 / 2));
        make.left.equalTo(self.ShowAlertView).with.offset(0);
        make.right.equalTo(self.ShowAlertView.mas_right).with.offset(0);
        make.height.mas_equalTo(SYRealValue(50 / 2));
    }];
    
    CAKeyframeAnimation * animation;
    animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    animation.duration = 0.5;
    animation.removedOnCompletion = YES;
    animation.fillMode = kCAFillModeForwards;
    
    NSMutableArray *values = [NSMutableArray array];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.1, 1.1, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9, 0.9, 0.9)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    
    animation.values = values;
    [self.ShowAlertView.layer addAnimation:animation forKey:nil];
    
    //取消按钮的背景色
    UIImage *CancelButtonNormalImage = [PublicClass imageWithColor:[UIColor whiteColor]];
    UIImage *CancelButtonSelectedImage = [PublicClass imageWithColor:[UIColor colorWithRed:0.89 green:0.90 blue:0.92 alpha:1]];
    
    self.CancelButton = [[UIButton alloc]initWithFrame:CGRectMake(0, SYRealValue(182 / 2), SYRealValue(315 / 2), SYRealValue(88 / 2))];
    [self.CancelButton setBackgroundImage:CancelButtonNormalImage forState:UIControlStateNormal];
    [self.CancelButton setBackgroundImage:CancelButtonSelectedImage forState:UIControlStateHighlighted];
    [self.CancelButton setTitle:self.CancelString forState:UIControlStateNormal];
    [self.CancelButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.CancelButton.titleLabel.font = [UIFont systemFontOfSize:SYRealValue(40 / 2)];
    [self.CancelButton addTarget:self action:@selector(ButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.CancelButton.tag = 0;
    [self.ShowAlertView addSubview:self.CancelButton];
    
    //给Button设置左下圆角
    UIBezierPath *CancelButtonMaskPath = [UIBezierPath bezierPathWithRoundedRect:self.CancelButton.bounds byRoundingCorners:UIRectCornerBottomLeft cornerRadii:CGSizeMake(5, 5)];
    CAShapeLayer *CancelButtonMaskLayer = [[CAShapeLayer alloc] init];
    CancelButtonMaskLayer.frame = self.CancelButton.bounds;
    CancelButtonMaskLayer.path = CancelButtonMaskPath.CGPath;
    self.CancelButton.layer.mask = CancelButtonMaskLayer;
    
    //给Button添加上边框
    CALayer *lineLayer = [CALayer layer];
    lineLayer.backgroundColor = [[UIColor colorWithRed:0.89 green:0.90 blue:0.92 alpha:1] CGColor];
    lineLayer.frame = CGRectMake(0, 0, SYRealValue(315 / 2), 0.5);
    [self.CancelButton.layer addSublayer:lineLayer];
    
    //确定按钮的背景色
    UIImage *OkButtonImage = [PublicClass imageWithColor:[UIColor colorWithRed:0.94 green:0.20 blue:0.22 alpha:1]];
    
    self.OkButton = [[UIButton alloc]initWithFrame:CGRectMake(SYRealValue(317 / 2), SYRealValue(182 / 2), SYRealValue(315 / 2), SYRealValue(88 / 2))];
    [self.OkButton setBackgroundImage:OkButtonImage forState:UIControlStateNormal];
    [self.OkButton setTitle:self.OkString forState:UIControlStateNormal];
    [self.OkButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.OkButton.titleLabel.font = [UIFont systemFontOfSize:SYRealValue(40 / 2)];
    [self.OkButton addTarget:self action:@selector(ButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.OkButton.tag = 1;
    [self.ShowAlertView addSubview:self.OkButton];
    
    //给Button设置右下圆角
    UIBezierPath *OkButtonMaskPath = [UIBezierPath bezierPathWithRoundedRect:self.OkButton.bounds byRoundingCorners:UIRectCornerBottomRight cornerRadii:CGSizeMake(5, 5)];
    CAShapeLayer *OkButtonMaskLayer = [[CAShapeLayer alloc] init];
    OkButtonMaskLayer.frame = self.OkButton.bounds;
    OkButtonMaskLayer.path = OkButtonMaskPath.CGPath;
    self.OkButton.layer.mask = OkButtonMaskLayer;
}

- (void)Show {
    UIView *KeyWindow = [[UIApplication sharedApplication] keyWindow];
    [KeyWindow addSubview:self];
}

- (void)ButtonAction:(UIButton *)sender {
    if ([self.delegate respondsToSelector:@selector(CustomAlertView:clickedButtonAtIndex:)]) {
        [self.delegate CustomAlertView:self clickedButtonAtIndex:(sender.tag)];
    }
    
    self.alpha = 0.0;
    [self removeFromSuperview];
    self.backgroundView = nil;
    self.ShowAlertView = nil;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
